package com.zj.sixtyDayChallenge.list;

/**
 * @program: algorithm
 * @description: 两两交换链表中的节点
 * 24 swap-nodes-in-pairs
 * @author: Zhang Bo
 * @create: 2023-02-09 11:20:56
 **/
public class SwapNodesInPairs {
    public static void main(String[] args) {
        Solution solution = new SwapNodesInPairs().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    class Solution {
        public ListNode swapPairs(ListNode head) {

            // 虚拟头指针
            ListNode newHead = new ListNode(-1);
            newHead.next = head;
            ListNode cur = newHead;

            while (cur.next != null && cur.next.next != null) {
                ListNode one = cur.next;
                ListNode two = cur.next.next;
                ListNode three = cur.next.next.next;
                cur.next = two;
                two.next = one;
                one.next = three;
                cur = one;
                print(cur);
            }
            return newHead.next;
        }

        public void print(ListNode head) {
            ListNode cur = head;
            while (cur != null) {
                System.out.print(cur.val);
                cur = cur.next;
            }
            System.out.println();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}